package com.spontaneappnew.adapters;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.spontaneappnew.ImageUtils;
import com.spontaneappnew.Place;
import com.spontaneappnew.Places;
import com.spontaneappnew.R;

public class CustomInfoWindowAdapter implements InfoWindowAdapter{
	private Activity activity;
	
	public CustomInfoWindowAdapter(Activity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
	}

	@Override
	public View getInfoContents(Marker marker) {	
		View result = activity.getLayoutInflater().inflate(R.layout.info_window, null);
		Places places = Places.getInstance();
		RatingBar rating = (RatingBar) result.findViewById(R.id.info_window_rating);
		TextView name = (TextView) result.findViewById(R.id.info_window_name); 
		TextView description = (TextView) result.findViewById(R.id.info_window_description); 
		ImageView thumb = (ImageView) result.findViewById(R.id.info_window_image);
		for (Place place : places.getAllPlaces()) {
			if (place.getId().compareTo(marker.getTitle())==0){
				name.setText(place.getName());
				description.setText(place.getDescription());
				rating.setRating(place.getRating());
				if (place.getNumOfImages()>0){
					ParseFile tmb = place.getThumbnailsArray().get(0);
					byte[] img = null;
					try {
						img = tmb.getData();
						
						thumb.setImageBitmap(new ImageUtils().getRoundedRectBitmap(BitmapFactory.decodeByteArray(img,0, img.length),20));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					
				}
				else {
					thumb.setVisibility(View.GONE); 
					rating.setPadding(0, 30, 0, 0);
				}
				break;
			}
		}

		return result;
	}

	@Override
	public View getInfoWindow(Marker arg0) {
		return null;
	}

}
